//
//  LGJoinTeamController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/16.
//  加入团队

#import "LGJoinTeamController.h"
//团队详情
#import "LGTeamDetailController.h"
//发送申请弹框
#import "LGJoinTeamAlertView.h"

#import "LGJoinTeamCell.h"
#import "SDImageCache.h"
@interface LGJoinTeamController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITextField *searchTF;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *sendBtn;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSMutableArray *resultArray;

@end

@implementation LGJoinTeamController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"加入团队";
    [self.view addSubview:self.searchTF];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.sendBtn];
    [self emptyViewWithSuperView:self.view frame:CGRectMake(0, viewPix(70), Screen_W, Screen_H-topBarHeight-viewPix(70))];
    [self requestData];
    
    [[SDImageCache sharedImageCache] clearWithCacheType:SDImageCacheTypeAll completion:^{
        [self.tableView reloadData];
    }];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/my.teams/teamsList" parameters:@{@"keyword":self.searchTF.text} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName content:@"暂无团队" offset:0];
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        self.isfirst = NO;
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName content:LGErrorMessage offset:0];
        }
    }];
}

-(void)searchTeamAction:(UITextField *)textField{
    [self requestData];
}

-(void)sendBtnAction{
    if(self.sendBtn.selected == NO){
        NSString *content = [NSString stringWithFormat:@"确定申请加入选中的 %ld 个团队吗？",self.resultArray.count];
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:content];
        [attrStr addAttributes:@{NSFontAttributeName:LGFontWeight(16, UIFontWeightMedium),NSForegroundColorAttributeName:[UIColor colorWithString:@"#429BFF"]} range:NSMakeRange(10, content.length-16)];
        __weak typeof(self) weakSelf = self;
        [LGAlertTool alertWithTitle:@"发送申请" attrbutContent:attrStr sureAction:^{
            NSString *teamIds = self.resultArray[0];
            if(self.resultArray.count>1){
                for (NSInteger i=1; i<self.resultArray.count; i++) {
                    teamIds = [NSString stringWithFormat:@"%@,%@",teamIds,self.resultArray[i]];
                }
            }
            [RequestUtil withPOST:@"/doctor/my.teams/intoTeam" parameters:@{@"teamids":teamIds} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
                if (code == 200) {
                    [TooltipView showMessage:@"发送成功" offset:0];
                    [weakSelf requestData];
                }
            } error:^(NSError *error) {
                
            }];
        }];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGJoinTeamCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lineView.hidden = (indexPath.row == self.dataArray.count-1)?YES:NO;
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    NSString *teamId = LGNSString(tempDic[@"teamid"]);
    cell.dataDic = tempDic;
    cell.isSelect = [self.resultArray containsObject:teamId]?YES:NO;
    __weak typeof(self) weakSelf = self;
    cell.selectTeamAction = ^(NSString * _Nonnull teamid) {
        if(![weakSelf.resultArray containsObject:teamid]){
            [weakSelf.resultArray addObject:teamid];
        }else{
            [weakSelf.resultArray removeObject:teamid];
        }
        [weakSelf.tableView reloadData];
        weakSelf.sendBtn.selected = (weakSelf.resultArray.count>0)?NO:YES;
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    NSString *teamId = LGNSString(tempDic[@"teamid"]);
    LGTeamDetailController *controller = [[LGTeamDetailController alloc]init];
    controller.teamId = teamId;
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark
#pragma mark ====> 创建控件
-(NSMutableArray *)resultArray{
    if(!_resultArray){
        _resultArray = [NSMutableArray array];
    }
    return _resultArray;
}
-(UITextField *)searchTF{
    if(!_searchTF){
        _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(54))];
        _searchTF.layer.shadowColor = [UIColor colorWithString:@"#B3C5D9"].CGColor;
        _searchTF.layer.shadowOffset = CGSizeMake(0, 2);
        _searchTF.layer.shadowOpacity = 0.15;
        _searchTF.layer.cornerRadius = 15;
        _searchTF.backgroundColor = [UIColor whiteColor];
        _searchTF.tintColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textAlignment = NSTextAlignmentLeft;
        _searchTF.keyboardType = UIKeyboardTypeDefault;
        _searchTF.placeholder = @"搜索";
        _searchTF.font = LGFont(16);
        _searchTF.leftViewMode = UITextFieldViewModeAlways;
        _searchTF.leftView = [self leftView];
        [_searchTF addTarget:self action:@selector(searchTeamAction:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchTF;
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(70), Screen_W, Screen_H-topBarHeight-viewPix(70)-viewPix(90)) style:UITableViewStylePlain];
        [_tableView registerClass:[LGJoinTeamCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.rowHeight = viewPix(60);
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(25))];
        _tableView.tableFooterView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(25))];
    }
    return _tableView;
}

-(UIButton *)sendBtn{
    if(!_sendBtn){
        _sendBtn = [UIButton buttonWithTitle:@"发送申请" topMargin:Screen_H-topBarHeight-viewPix(85)];
        _sendBtn.selected = YES;
        [_sendBtn addTarget:self action:@selector(sendBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendBtn;
}

-(UIView *)leftView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(36), viewPix(54))];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"searchGray16"]];
    imageView.frame = CGRectMake(viewPix(10), viewPix(19), viewPix(16), viewPix(16));
    [baseView addSubview:imageView];
    return baseView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
